*******************************************************************************
*** Description: 	This document provides the code for reproducing the 	***
***					tables and resulting figures of the paper, "Not All     ***
***					Election Winners Are Equal: Satisfaction with Democracy ***
***					and the Nature of the Vote," which is authored	  		***
***				    by Shane P. Singh and appears in the 					***
***					European Journal of Political Resarch in 2014. 			***
*******************************************************************************

**************
**************
*Set the Version, Install Packages, and Open the Data                                                                                                                                
**************
**************
version 13.1

ssc install parmest
net install grc1leg, from(http://www.stata.com/users/vwiggins/)

use "/Users/singh/Dropbox/Unshared/PLS/Research/Satisfaction, Winning, and Choice/replication/CSES Modules 1 and 2.dta" //*Here and elsewhere, adjust the file pathway



**************
**************
*Model 1 of Table 1 and Top Panel of Figure 2
**************
**************
xtlogit satdich c.winner##c.voted_ideo abstained educ efficacy majoritarian_prez freedom gdppercapPPP gdpgrowth CPI prez, i(cntryyearnum) intmethod(ghermite)  intpoints(5) 
margins, at(winner = (0(1)1) voted_ideo = (0(1)1)) post predict(pu0)


parmest, format(estimate min95 max95) saving("/Users/singh/Desktop/M1temp.dta", replace) 

preserve

use "/Users/singh/Desktop/M1temp.dta", clear

gen n = _n

twoway  (scatter n estimate in 4, mcolor(black) msymbol(circle)) (rcap  min95 max95 n in 4, horizontal lcolor(black)) ///
		(scatter n estimate in 3, mcolor(red) msymbol(circle)) (rcap  min95 max95 n in 3, horizontal lcolor(red)) ///
		(scatter n estimate in 2, mcolor(blue) msymbol(circle)) (rcap  min95 max95 n in 2, horizontal lcolor(blue)) ///
		(scatter n estimate in 1, mcolor(green) msymbol(circle)) (rcap  min95 max95 n in 1, horizontal lcolor(green)), ///
																scheme(s1mono) ///
		legend(order(1 "Optimal Winners" 3 "Non-Optimal Winners" 5 "Optimal Losers" 7 "Non-Optimal Losers") rows(2)) xscale(range(.55 .75)) xlabel(.55[.05].75) ///
		xtitle(Pr(Satisfied with Democracy)) title("Ideology") ytitle("") ylabel(none) saving(/Users/singh/Desktop/M1, replace) ///

restore



**************
**************
*Model 2 of Table 1 and Middle Panel of Figure 2
**************
**************
xtlogit satdich c.winner##voted_affect abstained educ efficacy majoritarian_prez freedom gdppercapPPP gdpgrowth CPI prez, i(cntryyearnum) intmethod(ghermite)  intpoints(5) 
margins, at(winner = (0(1)1) voted_affect = (0(1)1)) post predict(pu0)



parmest, format(estimate min95 max95) saving("/Users/singh/Desktop/M2temp.dta", replace)

preserve

use "/Users/singh/Desktop/M2temp.dta", clear

gen n = _n

twoway  (scatter n estimate in 4, mcolor(black) msymbol(circle)) (rcap  min95 max95 n in 4, horizontal lcolor(black)) ///
		(scatter n estimate in 3, mcolor(red) msymbol(circle)) (rcap  min95 max95 n in 3, horizontal lcolor(red)) ///
		(scatter n estimate in 2, mcolor(blue) msymbol(circle)) (rcap  min95 max95 n in 2, horizontal lcolor(blue)) ///
		(scatter n estimate in 1, mcolor(green) msymbol(circle)) (rcap  min95 max95 n in 1, horizontal lcolor(green)), ///
																scheme(s1mono) ///
		legend(order(1 "Optimal Winners" 3 "Non-Optimal Winners" 5 "Optimal Losers" 7 "Non-Optimal Losers") rows(2)) xscale(range(.55 .75)) xlabel(.55[.05].75) ///
		xtitle(Pr(Satisfied with Democracy)) title("Party Affect") ytitle("") ylabel(none) saving(/Users/singh/Desktop/M2, replace) ///

restore


**************
**************
*Model 3 of Table 1 and Bottom Panel of Figure 2
**************
**************
xtlogit satdich c.winner##voted_ID abstained educ efficacy majoritarian_prez freedom gdppercapPPP gdpgrowth CPI prez, i(cntryyearnum) intmethod(ghermite)  intpoints(5) 
margins, at(winner = (0(1)1) voted_ID = (0(1)1)) post predict(pu0)


parmest, format(estimate min95 max95) saving("/Users/singh/Desktop/M3temp.dta", replace)

preserve

use "/Users/singh/Desktop/M3temp.dta", clear

gen n = _n

twoway  (scatter n estimate in 4, mcolor(black) msymbol(circle)) (rcap  min95 max95 n in 4, horizontal lcolor(black)) ///
		(scatter n estimate in 3, mcolor(red) msymbol(circle)) (rcap  min95 max95 n in 3, horizontal lcolor(red)) ///
		(scatter n estimate in 2, mcolor(blue) msymbol(circle)) (rcap  min95 max95 n in 2, horizontal lcolor(blue)) ///
		(scatter n estimate in 1, mcolor(green) msymbol(circle)) (rcap  min95 max95 n in 1, horizontal lcolor(green)), ///
																scheme(s1mono) ///
		legend(order(1 "Optimal Winners" 3 "Non-Optimal Winners" 5 "Optimal Losers" 7 "Non-Optimal Losers") rows(2)) xscale(range(.55 .75)) xlabel(.55[.05].75)  ///
		xtitle(Pr(Satisfied with Democracy)) title("Party ID") ytitle("") ylabel(none)  saving(/Users/singh/Desktop/M3, replace)
restore


**************
**************
*Figure 2
**************
**************
grc1leg "/Users/singh/Desktop/M1.gph" "/Users/singh/Desktop/M2.gph" "/Users/singh/Desktop/M3.gph", xcommon rows(3) 




**************
**************
*Model 4 of Table 1 and Figure 3
**************
**************
xtlogit satdich c.winner##c.optimality abstained educ efficacy majoritarian_prez freedom gdppercapPPP gdpgrowth CPI prez, i(cntryyearnum) intmethod(ghermite)  intpoints(5) 
margins, at(winner = (0(1)1) optimality = (0(1)3)) post predict(pu0)


mat t=J(8,3,.)

mat a = (0\1\2\3\0\1\2\3)                        /* get the 4 "at" values    */

forvalues i=1/8 {
  mat t[`i',1] = _b[`i'._at]                       /* get probability estimates */
  mat t[`i',2] = _b[`i'._at] - 1.96*_se[`i'._at]   /* compute lower limit       */
  mat t[`i',3] = _b[`i'._at] + 1.96*_se[`i'._at]   /* compute upper limit       */
}

mat t=t,a                                          /* horizontal concatenation  */
mat colnames t = prob ll ul at                     /* fix column names          */
svmat t, names(col)                                /* save matrix as data       */

twoway (rarea ll ul at in 5/8, color(gs2)) (line prob at in 5/8, lcolor(black))  (rarea ll ul at in 1/4, color(blue)) (line prob at in 1/4, lcolor(black)),   ///
		legend(order(1 "Winners" 3 "Losers") rows(1))  ///
     xtitle(Optimality) ytitle(Pr(Satisfied with Democracy))  scheme(s1mono)  ///
	
       
drop prob-at
	   

**************
**************
*Figure 4
**************
**************

xtlogit satdich c.majoritarian_prez##c.winner##voted_ideo abstained educ efficacy freedom gdppercapPPP gdpgrowth CPI prez  , i(cntryyearnum) intmethod(ghermite)  intpoints(5) 
estimates table, b(%9.3f)  p(%9.3f)
margins, at(winner = (0(1)1) voted_ideo = (0(1)1) majoritarian_prez= (0(1)1)) post predict(pu0)


parmest, format(estimate min95 max95) saving("/Users/singh/Desktop/graphtemp.dta", replace)

preserve

use "/Users/singh/Desktop/graphtemp.dta", clear

gen n = _n

twoway  (scatter n estimate in 8, mcolor(black) msymbol(circle)) (rcap  min95 max95 n in 8, horizontal lcolor(black)) ///
		(scatter n estimate in 7, mcolor(red) msymbol(circle)) (rcap  min95 max95 n in 7, horizontal lcolor(red)) ///
		(scatter n estimate in 6, mcolor(blue) msymbol(circle)) (rcap  min95 max95 n in 6, horizontal lcolor(blue)) ///
		(scatter n estimate in 5, mcolor(green) msymbol(circle)) (rcap  min95 max95 n in 5, horizontal lcolor(green)), ///
																		scheme(s1mono) ///
		legend(order(1 "Optimal Winners" 3 "Non-Optimal Winners"  /// 
					 5 "Optimal Losers" 7 "Non-Optimal Losers") ///	 			 
					 rows(2))  ///
		xtitle(Pr(Satisfied with Democracy)) title("") title("Majoritarian Systems") ylabel(none) ytitle("") xscale(range(.50 .75)) xlabel(.50[.05].75) ///
		
graph save Graph "/Users/singh/Desktop/Graph1.gph", replace
		
		
twoway  (scatter n estimate in 4, mcolor(black) msymbol(circle)) (rcap  min95 max95 n in 4, horizontal lcolor(black)) ///
		(scatter n estimate in 3, mcolor(red) msymbol(circle)) (rcap  min95 max95 n in 3, horizontal lcolor(red)) ///
		(scatter n estimate in 2, mcolor(blue) msymbol(circle)) (rcap  min95 max95 n in 2, horizontal lcolor(blue)) ///
		(scatter n estimate in 1, mcolor(green) msymbol(circle)) (rcap  min95 max95 n in 1, horizontal lcolor(green)), ///
																scheme(s1mono) ///
		legend(order(1 "Optimal Winners" 3 "Non-Optimal Winners" ///
					 5 "Optimal Losers" 7 "Non-Optimal Losers") ///	 			 
					 rows(2))  ///
		xtitle(Pr(Satisfied with Democracy)) title("") title("Non-Majoritarian Systems") ylabel(none) ytitle("") xscale(range(.50 .75)) xlabel(.50[.05].75) ///

graph save Graph "/Users/singh/Desktop/Graph2.gph", replace


grc1leg "/Users/singh/Desktop/Graph1.gph" "/Users/singh/Desktop/Graph2.gph", xcommon  title(Ideology, color(black)) rows(2) saving(/Users/singh/Desktop/graphcom1temp, replace)

restore





xtlogit satdich c.majoritarian_prez##c.winner##c.voted_affect abstained educ efficacy freedom gdppercapPPP gdpgrowth CPI prez , i(cntryyearnum) intmethod(ghermite)  intpoints(5) 
estimates table, b(%9.3f)  p(%9.3f)
margins, at(winner = (0(1)1) voted_affect = (0(1)1) majoritarian_prez= (0(1)1)) post predict(pu0)



parmest, format(estimate min95 max95) saving("/Users/singh/Desktop/graphtemp.dta", replace)

preserve

use "/Users/singh/Desktop/graphtemp.dta", clear

gen n = _n

twoway  (scatter n estimate in 8, mcolor(black) msymbol(circle)) (rcap  min95 max95 n in 8, horizontal lcolor(black)) ///
		(scatter n estimate in 7, mcolor(red) msymbol(circle)) (rcap  min95 max95 n in 7, horizontal lcolor(red)) ///
		(scatter n estimate in 6, mcolor(blue) msymbol(circle)) (rcap  min95 max95 n in 6, horizontal lcolor(blue)) ///
		(scatter n estimate in 5, mcolor(green) msymbol(circle)) (rcap  min95 max95 n in 5, horizontal lcolor(green)), ///
																		scheme(s1mono) ///
		legend(order(1 "Optimal Winners" 3 "Non-Optimal Winners"  /// 
					 5 "Optimal Losers" 7 "Non-Optimal Losers") ///	 			 
					 rows(2))  ///
		xtitle(Pr(Satisfied with Democracy)) title("") title("Majoritarian Systems") ylabel(none) ytitle("") xscale(range(.50 .75)) xlabel(.50[.05].75) ///
		
graph save Graph "/Users/singh/Desktop/Graph1.gph", replace
		
		
twoway  (scatter n estimate in 4, mcolor(black) msymbol(circle)) (rcap  min95 max95 n in 4, horizontal lcolor(black)) ///
		(scatter n estimate in 3, mcolor(red) msymbol(circle)) (rcap  min95 max95 n in 3, horizontal lcolor(red)) ///
		(scatter n estimate in 2, mcolor(blue) msymbol(circle)) (rcap  min95 max95 n in 2, horizontal lcolor(blue)) ///
		(scatter n estimate in 1, mcolor(green) msymbol(circle)) (rcap  min95 max95 n in 1, horizontal lcolor(green)), ///
																scheme(s1mono) ///
		legend(order(1 "Optimal Winners" 3 "Non-Optimal Winners" ///
					 5 "Optimal Losers" 7 "Non-Optimal Losers") ///	 			 
					 rows(2))  ///
		xtitle(Pr(Satisfied with Democracy)) title("") title("Non-Majoritarian Systems") ylabel(none) ytitle("") xscale(range(.50 .75)) xlabel(.50[.05].75) ///

graph save Graph "/Users/singh/Desktop/Graph2.gph", replace


grc1leg "/Users/singh/Desktop/Graph1.gph" "/Users/singh/Desktop/Graph2.gph", xcommon  title(Party Affect, color(black)) rows(2) saving(/Users/singh/Desktop/graphcom2temp, replace) 

restore


xtlogit satdich c.majoritarian_prez##c.winner##c.voted_ID abstained educ efficacy freedom gdppercapPPP gdpgrowth CPI prez  , i(cntryyearnum) intmethod(ghermite)  intpoints(5) 
estimates table, b(%9.3f)  p(%9.3f)
margins, at(winner = (0(1)1) voted_ID = (0(1)1) majoritarian_prez= (0(1)1)) post predict(pu0)




parmest, format(estimate min95 max95) saving("/Users/singh/Desktop/graphtemp.dta", replace)

preserve

use "/Users/singh/Desktop/graphtemp.dta", clear

gen n = _n

twoway  (scatter n estimate in 8, mcolor(black) msymbol(circle)) (rcap  min95 max95 n in 8, horizontal lcolor(black)) ///
		(scatter n estimate in 7, mcolor(red) msymbol(circle)) (rcap  min95 max95 n in 7, horizontal lcolor(red)) ///
		(scatter n estimate in 6, mcolor(blue) msymbol(circle)) (rcap  min95 max95 n in 6, horizontal lcolor(blue)) ///
		(scatter n estimate in 5, mcolor(green) msymbol(circle)) (rcap  min95 max95 n in 5, horizontal lcolor(green)), ///
																		scheme(s1mono) ///
		legend(order(1 "Optimal Winners" 3 "Non-Optimal Winners"  /// 
					 5 "Optimal Losers" 7 "Non-Optimal Losers") ///	 			 
					 rows(2))  ///
		xtitle(Pr(Satisfied with Democracy)) title("") title("Majoritarian Systems") ylabel(none) ytitle("") xscale(range(.50 .75)) xlabel(.50[.05].75) ///
		
graph save Graph "/Users/singh/Desktop/Graph1.gph", replace
		
		
twoway  (scatter n estimate in 4, mcolor(black) msymbol(circle)) (rcap  min95 max95 n in 4, horizontal lcolor(black)) ///
		(scatter n estimate in 3, mcolor(red) msymbol(circle)) (rcap  min95 max95 n in 3, horizontal lcolor(red)) ///
		(scatter n estimate in 2, mcolor(blue) msymbol(circle)) (rcap  min95 max95 n in 2, horizontal lcolor(blue)) ///
		(scatter n estimate in 1, mcolor(green) msymbol(circle)) (rcap  min95 max95 n in 1, horizontal lcolor(green)), ///
																scheme(s1mono) ///
		legend(order(1 "Optimal Winners" 3 "Non-Optimal Winners" ///
					 5 "Optimal Losers" 7 "Non-Optimal Losers") ///	 			 
					 rows(2))  ///
		xtitle(Pr(Satisfied with Democracy)) title("") title("Non-Majoritarian Systems") ylabel(none) ytitle("") xscale(range(.50 .75)) xlabel(.50[.05].75) ///

graph save Graph "/Users/singh/Desktop/Graph2.gph", replace


grc1leg "/Users/singh/Desktop/Graph1.gph" "/Users/singh/Desktop/Graph2.gph", xcommon  title(Party ID, color(black)) rows(2) saving(/Users/singh/Desktop/graphcom3temp, replace) 

restore



**combine all three
grc1leg "/Users/singh/Desktop/graphcom1temp.gph" "/Users/singh/Desktop/graphcom2temp.gph" "/Users/singh/Desktop/graphcom3temp.gph", xcommon rows(1)
*this can be squished in graph editor by changing the aspect ratio to .325





**************
**************
*Figure 5
**************
**************
xtlogit satdich c.majoritarian_prez##c.winner##c.optimality abstained educ efficacy freedom gdppercapPPP gdpgrowth CPI prez, i(cntryyearnum) intmethod(ghermite)  intpoints(5) 
margins, at(winner = (0(1)1) majoritarian_prez= (0(1)1) optimality = (0(1)3)) post predict(pu0)


mat t=J(16,3,.)

mat a = (0\1\2\3\0\1\2\3\0\1\2\3\0\1\2\3)                        /* get the 16 "at" values for non-maj and for maj    */

forvalues i=1/16 {
  mat t[`i',1] = _b[`i'._at]                       /* get probability estimates */
  mat t[`i',2] = _b[`i'._at] - 1.96*_se[`i'._at]   /* compute lower limit       */
  mat t[`i',3] = _b[`i'._at] + 1.96*_se[`i'._at]   /* compute upper limit       */
}

mat t=t,a                                          /* horizontal concatenation  */
mat colnames t = prob ll ul at                     /* fix column names          */
svmat t, names(col)                                /* save matrix as data       */



twoway  (rarea ll ul at in 13/16, color(gs2)) (line prob at in 13/16, lcolor(black)) (rarea ll ul at in 5/8, color(red)) (line prob at in 5/8, lcolor(black)) ///
	   (rarea ll ul at in 9/12, color(blue)) (line prob at in 9/12, lcolor(black)) (rarea ll ul at in 1/4, color(green)) (line prob at in 1/4, lcolor(black)) , ///
		legend(order(1 "Winners in Majoritarian Electoral Systems" 3 "Winners in Non-Majoritarian Electoral Systems" 5 "Losers in Majoritarian Electoral Systems" 7 "Losers in Non-Majoritarian Electoral Systems") rows(4))  ///
     	xtitle(Optimality) ytitle(Pr(Satisfied with Democracy))  scheme(s1mono)  yscale(range(.50 .75)) ylabel(.50[.05].75) ///
       
       
drop prob-at
      


**************
**************
*Figure 6
**************
**************
gen loser = 1 - winner


reg optimality c.winner##c.majoritarian_prez, cl(cntryyear)
parmest, format(estimate min95 max95) saving("/Users/singh/Desktop/graphtemp7.dta", replace)
reg optimality c.loser##c.majoritarian_prez, cl(cntryyear)
parmest, format(estimate min95 max95) saving("/Users/singh/Desktop/graphtemp8.dta", replace)

reg voted_ID c.winner##c.majoritarian_prez, cl(cntryyear)
parmest, format(estimate min95 max95) saving("/Users/singh/Desktop/graphtemp5.dta", replace)
reg voted_ID c.loser##c.majoritarian_prez, cl(cntryyear)
parmest, format(estimate min95 max95) saving("/Users/singh/Desktop/graphtemp6.dta", replace)

reg voted_affect c.winner##c.majoritarian_prez, cl(cntryyear)
parmest, format(estimate min95 max95) saving("/Users/singh/Desktop/graphtemp3.dta", replace)
reg voted_affect c.loser##c.majoritarian_prez, cl(cntryyear)
parmest, format(estimate min95 max95) saving("/Users/singh/Desktop/graphtemp4.dta", replace)

reg voted_ideo c.winner##c.majoritarian_prez, cl(cntryyear)
parmest, format(estimate min95 max95) saving("/Users/singh/Desktop/graphtemp.dta", replace)
reg voted_ideo c.loser##c.majoritarian_prez, cl(cntryyear)
parmest, format(estimate min95 max95) saving("/Users/singh/Desktop/graphtemp2.dta", replace)

drop loser



preserve
use "/Users/singh/Desktop/graphtemp.dta", clear 
append using "/Users/singh/Desktop/graphtemp2.dta"
append using "/Users/singh/Desktop/graphtemp3.dta"
append using "/Users/singh/Desktop/graphtemp4.dta"
append using "/Users/singh/Desktop/graphtemp5.dta"
append using "/Users/singh/Desktop/graphtemp6.dta"
append using "/Users/singh/Desktop/graphtemp7.dta"
append using "/Users/singh/Desktop/graphtemp8.dta"

gen index = .
replace index = 1 in 2
replace index = 1 in 6
replace index = 2 in 10
replace index = 2 in 14
replace index = 3 in 18
replace index = 3 in 22
replace index = 4 in 26
replace index = 4 in 30
drop if index == .

gen WINDIF = estimate
bysort index: replace WINDIF = WINDIF[_n+1]
gen LOSEDIF = estimate
replace LOSEDIF = . if WINDIF == .

gen WINDIFmin95 = min95
bysort index: replace WINDIFmin95 = WINDIFmin95[_n+1]
gen LOSEDIFmin95 = min95

gen WINDIFmax95 = max95
bysort index: replace WINDIFmax95 = WINDIFmax95[_n+1]
gen LOSEDIFmax95 = max95

gen type = ""
replace type = "Ideology" in 1
replace type = "Ideology" in 2
replace type = "Party Affect" in 3
replace type = "Party Affect" in 4
replace type = "Party ID" in 5
replace type = "Party ID" in 6
replace type = "xOptimality" in 7
replace type = "xOptimality" in 8

gen n = .
replace n = 1 in 1
replace n = 2 in 2
replace n = 1 in 3
replace n = 2 in 4
replace n = 1 in 5
replace n = 2 in 6
replace n = 1 in 7
replace n = 2 in 8

save "/Users/singh/Desktop/graphtempappended.dta", replace



twoway  (scatter n estimate in 1/8,  ylabel(1(1)2, nogrid)  mcolor(black) msymbol(circle) msize(medsmall) ytitle("")   by(type, note("") style(compact)  graphregion(fcolor(white) ifcolor(white)) yrescale noxrescale iyaxes iytick ixaxes ixtick rows(6)))  (rcap  min95 max95 n in 1/8, horizontal lcolor(black) by(type)), ///
		xline(0) ///
	 	legend(order(1 "Difference in Means" "(Majoritarian minus Non-Majoritarian)" 2 "95% Confidence Interval") size(small) rows(2) )  /// 
		xlabel(, labsize(medsmall)) /// 
		ylabel(1 "Losers" 2 "Winners", angle(horizontal) labsize(small)) ///
		ysize(3.0) 

*use graph editor to get rid of the x, which was put in because "by" uses alphabetical sorting
		
restore
